package com.szh.mysobproject.ui.activity.msg

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.activityViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.dylanc.longan.lifecycleOwner
import com.szh.mysobproject.R
import com.szh.mysobproject.action.OnBack2TopListener
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.FishMsgListActivityBinding
import com.szh.mysobproject.ui.activity.FishPondDetailActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.MomentMsgAdapter
import com.szh.mysobproject.util.SimpleLinearSpaceItemDecoration
import com.szh.mysobproject.util.dp
import com.szh.mysobproject.util.loadStateListener
import com.szh.mysobproject.util.setDoubleClickListener
import com.szh.mysobproject.viewmodel.MsgViewModel
import com.szh.mysobproject.widget.StatusLayout
import kotlinx.coroutines.flow.collectLatest

/**
 * @author : create by  szh
 * @date : 2022/7/12 10:58
 * @desc :摸鱼消息列表
 */

class FishMsgListActivity : AppActivity(), StatusAction, OnBack2TopListener {

    private val mBinding by viewBinding<FishMsgListActivityBinding>()
    private val mMsgViewModel by viewModels<MsgViewModel>()
    private val mAdapterDelegate = AdapterDelegate()
    private val mMomentMagAdapter = MomentMsgAdapter(mAdapterDelegate)
    private val loadStateListener = loadStateListener(mMomentMagAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }


    override fun getLayoutId(): Int = R.layout.fish_msg_list_activity

    override fun initView() {
        mBinding.rvFishMsgList.apply {
            adapter = mMomentMagAdapter
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(SimpleLinearSpaceItemDecoration(4.dp))
        }
    }

    override fun initData() {
        //获取摸鱼未读消息
        lifecycleOwner.lifecycleScope.launchWhenCreated {
            mMsgViewModel.getMomentMsgList().collectLatest {
                mMomentMagAdapter.submitData(it)
            }
        }
    }

    override fun initEvent() {
        titleBar?.setDoubleClickListener {
            onBack2Top()
        }
        mBinding.refreshLayout.setOnRefreshListener {
            mMomentMagAdapter.refresh()
        }
        mMomentMagAdapter.addLoadStateListener(loadStateListener)
        mAdapterDelegate.setOnItemClickListener { _, position ->
            val item = mMomentMagAdapter.snapshot()[position] ?: return@setOnItemClickListener
            mMsgViewModel.readMomentMsg(item.id).observe(this) {}
            FishPondDetailActivity.start(this, item.momentId)
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.hlFishMsgHint

    override fun onDestroy() {
        super.onDestroy()
        mMomentMagAdapter.removeLoadStateListener(loadStateListener)
    }

    override fun onBack2Top() {
        mBinding.rvFishMsgList.smoothScrollToPosition(0)
    }
}